package com.snopy.user;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author snopy
 * @version 1.0.0
 * @classname Demo
 * @description
 * There was a problem importing one of the Python modules required to run yum
 *
 * @email 77912204@qq.com
 * @date 2022/08/04 18:42
 */
public class Demo {
    private static DataSource dataSource;
    public static void main(String[] args) throws ClassNotFoundException {
        Connection connection = null;
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        try
        {
            DataSource dataSource = Demo.getDataSource();
            connection = dataSource.getConnection();
            pstmt = connection.prepareStatement("SELECT * FROM stu");

            System.out.println("The Connection Object is of Class: " + connection.getClass());

            resultSet = pstmt.executeQuery();
            while (resultSet.next())
            {
                System.out.println(resultSet.getString(1) + "," + resultSet.getString(2) + "," + resultSet.getString(3));
            }

        }catch (Exception e) {
            try
            {
                connection.rollback();
            }
            catch (SQLException e1)
            {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }

    }
    public static DataSource getDataSource(){
        if (dataSource==null){
            HikariConfig config = new HikariConfig();
            config.setJdbcUrl("jdbc:mysql://192.168.164.103:3306/snopy102");
            config.setUsername("root");
            config.setPassword("!@#123QWEqwe");

            config.setMaximumPoolSize(10);
            config.setAutoCommit(false);
            config.addDataSourceProperty("cachePrepStmts", "true");
            config.addDataSourceProperty("prepStmtCacheSize", "250");
            config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

            dataSource = new HikariDataSource(config);
        }
        return dataSource;
    }
}
